<template>
  <view>
    <div class="cate-container">
      <!-- 左侧的滚动栏 -->
      <scroll-view scroll-y="true" class="scroll-view-left" :style="{height: vh +'px'}">
        <block v-for="(title,i) in cateList" :key="i">
          <view :class="['scroll-left-item', active === i?'active':'']" @click="changeActive(i)">{{title.cat_name}}
          </view>
        </block>
      </scroll-view>
      <!-- 右侧的滚动栏 -->
      <scroll-view scroll-y="true" class="scroll-view-right" :style="{height: vh +'px'}" :scroll-top="scrolltop">
        <view v-for="(item,i) in cateListlevel2" :key="i">
          <!-- 标题 -->
          <view class="catetitle">
            / {{item.cat_name}} /
          </view>
          <!-- 图签 -->
          <view class="cate-l3" @click="navigatorTo(item)">
            <view class="pic" v-for="(img,i2) in item.children" :key="i2">
              <image :src="img.cat_icon" mode=""></image>
              <text>{{img.cat_name}}</text>
            </view>
          </view>
        </view>
      </scroll-view>
    </div>
  </view>
</template>

<script>
  export default {
    onLoad() {
      //获取设备信息并获取当前可使用的宽度
      const res = uni.getSystemInfoSync()
      console.log(res);
      this.vh = res.windowHeight
      //获取分类信息方法
      this.getCateList()
    },
    data() {
      return {
        //分类数组
        cateList: [],
        //二级分类
        cateListlevel2: [],
        vh: 0,
        //判断选中的项
        active: 0,
        //滚动的距离
        scrolltop: 0
      };
    },
    methods: {
      async getCateList() {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/categories')
        if (res.meta.status !== 200) return uni.$showMsg()
        this.cateList = res.message
        this.cateListlevel2 = res.message[0].children
      },
      //修改选中项
      changeActive(i) {
        this.active = i
        this.cateListlevel2 = this.cateList[i].children
        //在0和1之间变化让vue可以检测到值变化了从而重置scrolltop
        this.scrolltop = this.scrolltop === 1 ? 0 : 1
      },
      //跳转到非tab页面goods-list
      navigatorTo(item){
        uni.navigateTo({
          url:"/subpages/goods-list/goods-list?cid="+item.cat_id
        })
      }
    }
  }
</script>

<style lang="scss">
  .cate-container {
    display: flex;

    .scroll-view-left {
      width: 120px;

      .scroll-left-item {
        background-color: #f7f7f7;
        line-height: 60px;
        text-align: center;
        font-size: 12px;

        &.active {
          position: relative;
          background-color: #ffffff;

          &::before {
            position: absolute;
            top: 50%;
            left: 0;
            transform: translateY(-50%);
            content: '';
            display: block;
            width: 3px;
            height: 30px;
            background-color: #c00000;
          }
        }
      }
    }

    .catetitle {
      font-size: 12px;
      font-weight: bold;
      text-align: center;
      padding: 15px 0;
    }

    .cate-l3 {
      display: flex;
      flex-wrap: wrap;

      .pic {
        width: 33.3%;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        margin-bottom: 10px;

        image {
          width: 60px;
          height: 60px;
        }

        text {
          font-size: 12px;
        }
      }
    }
  }
</style>
